package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductImage;
import com.qckj.amz.service.IProductImageService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品图片Controller
 *
 * @author luchuan
 * @since 1.0 2022-03-16
 */
@RestController
@RequestMapping("/amz/productImage")
public class ProductImageController extends BaseController {
    @Autowired
    private IProductImageService productImageService;

    /**
     * 获取产品图片详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productImage:query')")
    @GetMapping(value = "/{attId}")
    public AjaxResult getInfo(@PathVariable("attId") Long attId) {
        try {
            ProductImage productImage = productImageService.findById(attId);
            return AjaxResult.success(productImage);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增产品图片
     */
    @PreAuthorize("@ss.hasPermi('amz:productImage:add')")
    @Log(title = "产品图片", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductImage productImage) {
        try {
            productImageService.save(productImage);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除产品图片
     */
    @PreAuthorize("@ss.hasPermi('amz:productImage:remove')")
    @Log(title = "产品图片", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attIds}")
    public AjaxResult remove(@PathVariable Long[] attIds) {
        try {
            productImageService.deleteByIds(Arrays.asList(attIds));
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询产品图片列表
     * @param tableData
     * @return
     */
    /*@PreAuthorize("@ss.hasPermi('amz:productImage:list')")*/
    @PostMapping("/listProductImage")
    public TableDataInfo listProductImage(@RequestBody Map<String, Object> tableData) {
        Page<ProductImage> page = productImageService.listProductImage(tableData);
        return getDataTable(page);
    }

    /**
     * 删除附件管理表
     * @param ids String
     */
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    public AjaxResult delete(@PathVariable String ids) {
        try {
            List<Long> idList = StringUtils.stringToArrayLong(ids);
            productImageService.deleteByIds(idList);
            return AjaxResult.success();
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 更改图片目录
     * @param attrIdList    图片集合
     * @param pid           新目录pid
     * @return
     */
    @ApiOperation(value = "更改图片目录")
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    public AjaxResult move(@RequestParam(value = "attrId") List<Long> attrIdList,@RequestParam(value = "pid") Long pid) {
        try {
            AjaxResult res = productImageService.move(attrIdList,pid);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }



}
